<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <title>获取文件sha1</title>
        <script src="https://cdn.bootcdn.net/ajax/libs/js-sha1/0.6.0/sha1.min.js"></script>
    </head>
    <body>
        <select onchange="document.getElementById('upload_file').webkitdirectory=this.value;">
            <option value="">选择文件</option>
            <option value="1">选择文件夹</option>
        </select>
        <input id="upload_file" type="file" name="upload_filename" multiple="multiple">
        <input id="upload_submit" onclick="preup();" value="生成sha1" type="button">
        <button onclick="CopyAllDownloadUrl();">复制结果</button>
        <hr>
        <textarea id="result" style="width: 100%;"></textarea>
        <br>
        <button onclick="change2Tamper();">转换给油猴插件用</button><button onclick="change1Tamper();">将油猴插件结果转换</button>
        <div id="progress"></div>
        <script>
            var result1 = document.getElementById('result');
            function preup() {
                var files=document.getElementById('upload_file').files;
                //console.log(files);
                if (files.length<1) return;
                for (i=0;i<files.length;i++) {
                    var div=document.createElement('div');
                    div.setAttribute('id', (files[i].webkitRelativePath||files[i].name));
                    document.getElementById('progress').appendChild(div);
                    var div1=document.createElement('div');
                    div1.setAttribute('style', "display:inline-block");
                    div.appendChild(div1);
                    var div2=document.createElement('div');
                    div2.setAttribute('style', "display:inline-block");
                    div2.innerText = ', ' + (files[i].webkitRelativePath||files[i].name);
                    div.appendChild(div2);
                    readfileb(files[i], div1);
                }
            }
            function readfileb(file, div) {
                var reader = new FileReader();
                var spark = sha1.create();
                var chunksize=10*1024*1024;
                var asize = 0;
                reader.onprogress = function(e) {
                    div.innerText = ((asize + e.loaded)*100/file.size).toFixed(2) + " %";
                }
                reader.onload = function(e) {
                    spark.update(this.result);
                    asize += chunksize;
                    if (asize < file.size) {
                        readblob(asize);
                    } else {
                        let filesha1 = spark.hex();
                        var v = (file.webkitRelativePath||file.name) + "," + filesha1 + "," + file.size + "\n";
                        console.log(v);
                        result1.value += v;
                        result1.style.height = result1.scrollHeight + 'px';
                        div.parentNode.remove();
                    }
                }
                function readblob(start) {
                    let end = start + chunksize;
                    let blob = file.slice(start, end);
                    reader.readAsArrayBuffer(blob);
                }
                readblob(asize);
            }
            function CopyAllDownloadUrl() {
                result1.select();
                result1.setSelectionRange(0, result1.value.length);
                document.execCommand("copy");
                alert("复制成功，请粘贴");
            }
            function change2Tamper() {
                let e = result1.value.split(/\n/g);
                let t = e[0].split(',');
                if (t.length<3) {
                    alert("有误");
                    return;
                }
                result1.value = '';
                e.forEach(element => {
                    if (element!='') {
                        let a = element.split(',');
                        let b = new Object();
                        b.name = a[0];
                        b.content_hash = a[1];
                        b.size = parseInt(a[2]);
                        b.content_type = '';
                        let c = JSON.stringify(b);
                        c = encodeURIComponent(c);
                        c = unescape(c);
                        c = window.btoa(c);
                        result1.value += c + "\n";
                    }
                });
            }
            function change1Tamper() {
                let e = result1.value.split(/\n/g);
                try {
                    window.atob(e[0]);
                } catch {
                    alert("有误");
                    return;
                }
                result1.value = '';
                e.forEach(element => {
                    if (element!='') {
                        let a = decodeURIComponent(escape(window.atob(element)));
                        let b = JSON.parse(a);
                        result1.value += b.name + ',' + b.content_hash + ',' + b.size + "\n";
                    }
                });
            }
        </script>
    </body>
</html>
